package com.example.larou.mapper;

import cn.dev33.satoken.util.SaResult;
import com.example.larou.entity.Email;
import com.example.larou.entity.Item;
import com.example.larou.entity.Product;
import com.example.larou.entity.Store;
import org.apache.ibatis.annotations.*;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author Misaka  Mikoto
 */
public interface ItemMapper{

    @Insert("insert into larou.item(item_name,description,price,quantity,image,seller_id) " +
            "VALUES (#{item_name},#{description},#{price},#{quantity},#{image},#{seller_id})")
    boolean inserItem(@Param("item_name") String item_name ,
                      @Param("description") String description,
                      @Param("price") double price,
                      @Param("quantity") int quantity,
                      @Param("image") String image,
                      @Param("seller_id") int seller_id
    );

    @Delete("delete from larou.item where item_id = #{item_id}")
    boolean deleteItem(@Param("item_id") int item_id);

    @Update("update larou.item " +
            "set item_name = #{item_name} " +
            "where item_id = #{item_id}")
    boolean updateItem(@Param("item_id") Long item_id,@Param("item_name") String item_name);

    @Results(id = "itemMap", value = {
            @Result(property = "item_name", column = "item_name"),
            @Result(property = "seller_id",column = "seller_id"),
            @Result(property = "item_id",column = "item_id"),
    })
    @Select("select * from larou.item where #{item_name} is null  or  item_name  like concat('%', #{item_name}, '%')")
    List<Item> getAllItem(@Param("item_name") String item_name);



    @Results(id = "myItemMap", value = {
            @Result(property = "item_name", column = "item_name"),
            @Result(property = "seller_id",column = "seller_id"),
            @Result(property = "item_id",column = "item_id"),

    })
    @Select("select *from larou.item where seller_id=#{user_id}")
    List<Item> getMyItem(@Param("user_id") int user_id);
    @Insert("insert into larou.cart(user_id, item_id, quantity, price) " +
            "VALUES (#{user_id},#{item_id},#{quantity},#{price})")
    boolean addCart(@Param("user_id") int user_id ,
                      @Param("item_id") int item_id,
                      @Param("quantity") int quantity,
                      @Param("price") double price
    );
    @Insert("insert into larou.email(user_id, device_sender, recipient,email_name,content,device_id) " +
            "VALUES (#{user_id},#{device_sender},#{recipient},#{email_name},#{content},#{device_id})")
    boolean addEmail(@Param("user_id") int user_id ,
                    @Param("device_sender") String  sender,
                    @Param("recipient") String recipient,
                    @Param("email_name") String emailName,
                    @Param("content") String content, @Param("device_id") int device_id
    );
    @Results(id = "myCartMap", value = {
            @Result(property = "item_name", column = "item_name"),
            @Result(property = "seller_id",column = "seller_id"),
            @Result(property = "item_id",column = "item_id"),
            @Result(property = "cart_id",column = "cart_id"),
    })
   @Select("select larou.item.* ,larou.cart.cart_id from larou.item  join larou.cart  on larou.item.item_id = larou.cart.item_id" +
           " where larou.cart.user_id=#{user_id} ")
    List<Item> getMyCart(@Param("user_id") int user_id);
    @Delete("delete from larou.cart where cart_id = #{cart_id}")
    boolean deleteCartItem(@Param("cart_id") int cart_id);
    @Results(id = "myEmailMap", value = {
            @Result(property = "email_id", column = "email_id"),
            @Result(property = "user_id",column = "user_id"),
            @Result(property = "create_time",column = "create_time"),
            @Result(property = "update_time",column = "update_time"),
            @Result(property = "device_id",column = "device_id")
    })
    @Select("select * from larou.email where user_id=#{user_id} ")
    List<Email> getEmail(@Param("user_id") int user_id);


}
